/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import java.awt.Color;
import java.util.LinkedHashMap;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import net.worlds.ObjectList;
import net.worlds.PropertyDef;
import net.worlds.WorldsException;
import net.worlds.object.GameObject;


public class PropsTable extends JTable{
    private PropsTableModel model;
    public PropsTable()
    {
        super();
        this.setBackground(new Color(0, 0, 0, 0));
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                getTableHeader().setReorderingAllowed(false);
                initModel();
            }
        });
    }
    private void initModel()
    {
        model = new PropsTableModel();
        setModel(model);
        model.addColumn("Name");
        model.addColumn("Value");
        getColumnModel().getColumn(1).setCellRenderer(new PropsTableCellRenderer());
        getColumnModel().getColumn(0).setCellRenderer(new PropsTableCellRenderer());
        getColumnModel().getColumn(1).setCellEditor(new PropsTableCellEditor());
        getColumnModel().getColumn(0).setCellEditor(new PropsTableCellEditor());
    }
    public void changeKeyTo(String key, Object value)
    {
        for(int i = 0; i < model.getRowCount(); i++)
        {
            if(model.getValueAt(i, 0) == key)
            {
                model.setValueAtAuto(value, i, 1);
            }
        }
    }
    public void setObject(final GameObject[] objects) throws WorldsException
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                if(getModel().getRowCount() == 0 && objects.length == 0)
                    return;
                initModel();
                while(getModel().getRowCount() > 0)
                {
                    getModel().removeRow(0);
                }
                if(objects.length == 0)
                    return;
                Class t = objects[0].getClass();
                for(GameObject obj : objects)
                {
                    if(obj.getClass() != t)
                    {
                        return;
                    }
                }

                final LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) objects[0].getAllProperties().clone();
                for(GameObject obj : objects)
                {
                    for(String name : properties.keySet())
                    {
                        try {
                            if (obj != null && obj.getProp(name) == null || !obj.getProp(name).equals(properties.get(name))) {
                                properties.put(name, null);
                            }
                        } catch (WorldsException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                for(String p : properties.keySet())
                {
                    int classification = ObjectList.getPropDef(objects[0].getType(), p).classification;
                    if(classification == UI.propClass || classification == PropertyDef.CLASS_BOTH)
                       getModel().addRow(new Object[]{p, properties.get(p)});
                }
                model.addTableModelListener(new PropsTableListener());
            }
        });
    }
    @Override
    public DefaultTableModel getModel()
    {
        return model;
    }
    public void showProperties(GameObject obj)
    {
    }
}
